import {observable, action} from 'mobx';
import * as API_LIST from '../apis/meetingReport';
import {WeaTableNew} from 'comsMobx';
const {TableStore} = WeaTableNew;
import moment from 'moment';
import {forEach, isEmpty} from 'lodash';
export default class MeetingReport {
  
  @observable reportData = [];
  @observable reportCol = [];
  @observable exportExcelUrl = '';
  @observable tableStore = new TableStore();
  @observable col = 0;
  @observable row = -1;
  @observable type = '';
  @observable condition = [];
  @observable year = moment().year();
  @observable conditionUsed = [];
  @observable conditionParams = {
    orgType:'',
    subid: '',
    depid:'',
    yearType: '1',
    meetingType:'1',
  };//记录当前查询条件选项
  
  @action 
  setState = (params = {})=>{
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  getReportData = (params = {})=>{
    API_LIST.getReportData(params).then(data => {
      if(data.api_status) {
        this.reportData = data.datas;
        this.reportCol = data.columns;
        // this.setState({col: 0 ,row: -1});
      }
    })
  }

  @action
  getDetail = (params = {})=>{
    this.tableStore = new TableStore();
    API_LIST.getDetail(params).then(data => {
      data.api_status && this.tableStore.getDatas(data.sessionkey, 1);
    })
  }

  
  exportToExcel=()=> {
    //API_LIST.exportToExcel(params)
    let { orgType, subid,depid,yearType,meetingType } = this.conditionParams;
    let params = {
      orgType:orgType,
      subid: subid,
      depid:depid,
      yearType: yearType,
      meetingType:meetingType
    }
    this.exportExcelUrl  = encodeURI('/api/meeting/report/exportExcel?'+this.buildUrlStr(params)+'&_t='+new Date().getTime())
  }

  buildUrlStr = (params = {}) => {
    let str = "";
    _.forIn(params, (value, key) => {
        str += `&${key}=${value}`;
    });
    if (str !== "")
        str = str.substring(1);
    return str;
  }

  
  @action 
  getReportCondition = (params = {})=>{
    API_LIST.getReportCondition(params).then(data => {
      if(data.api_status){
        this.condition = data.config;
        let orgType = '', subid = '', replaceDatas = data.config[1].selectLinkageDatas['2'].browserConditionParam.replaceDatas;
        forEach(data.config[1].options, (option) => {
          if(option.selected) {
            orgType = option.key;
          }
        });
        let params = {
          ...this.conditionParams,
          orgType,
          subid: isEmpty(replaceDatas) ? '' : replaceDatas[0].id,
        }
        this.conditionParams = params;
        delete params.depid;
        this.getReportData(params);
      }
    })
  }

}